/*
========================================================================
 Name        : MdaAudioPlayerContainerView.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>
#include <MdaAudioPlayer.rsg>

#include "MdaAudioPlayer.hrh"
#include "MdaAudioPlayerContainerView.h"
#include "MdaAudioPlayerContainer.hrh"
#include "MdaAudioPlayerContainer.h"

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CMdaAudioPlayerContainerView::CMdaAudioPlayerContainerView()
	{
	iMdaAudioPlayerContainer = NULL;
	iPlayer = NULL;
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CMdaAudioPlayerContainerView::~CMdaAudioPlayerContainerView()
	{
	delete iMdaAudioPlayerContainer;
	iMdaAudioPlayerContainer = NULL;
	delete iPlayer;
	iPlayer = NULL;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CMdaAudioPlayerContainerView
 */
CMdaAudioPlayerContainerView* CMdaAudioPlayerContainerView::NewL()
	{
	CMdaAudioPlayerContainerView* self = CMdaAudioPlayerContainerView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CMdaAudioPlayerContainerView
 */
CMdaAudioPlayerContainerView* CMdaAudioPlayerContainerView::NewLC()
	{
	CMdaAudioPlayerContainerView* self = new ( ELeave ) CMdaAudioPlayerContainerView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CMdaAudioPlayerContainerView::ConstructL()
	{
	BaseConstructL( R_MDA_AUDIO_PLAYER_CONTAINER_MDA_AUDIO_PLAYER_CONTAINER_VIEW );
				
	iPlayer = CMp3Player::NewL();
	
	}

/**
 * @return The UID for this view
 */
TUid CMdaAudioPlayerContainerView::Id() const
	{
	return TUid::Uid( EMdaAudioPlayerContainerViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CMdaAudioPlayerContainerView::HandleCommandL( TInt aCommand )
	{
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{
		case EMdaAudioPlayerContainerViewPlayMenuItemCommand:
			{
			iPlayer->Play();
			}
			break;
		case EAknSoftkeyExit:
			{
			AppUi()->HandleCommandL( EEikCmdExit );
			}
			break;
		default:
			break;
		}
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CMdaAudioPlayerContainerView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	SetupStatusPaneL();

	if ( iMdaAudioPlayerContainer == NULL )
		{
		iMdaAudioPlayerContainer = CreateContainerL();
		iMdaAudioPlayerContainer->SetMopParent( this );
		AppUi()->AddToStackL( *this, iMdaAudioPlayerContainer );
		} 
	}

/**
 * 
 */
void CMdaAudioPlayerContainerView::DoDeactivate()
	{
	CleanupStatusPane();
	
	if ( iMdaAudioPlayerContainer != NULL )
		{
		AppUi()->RemoveFromViewStack( *this, iMdaAudioPlayerContainer );
		delete iMdaAudioPlayerContainer;
		iMdaAudioPlayerContainer = NULL;
		}	
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CMdaAudioPlayerContainerView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TInt result;
	TRAP( result, SetupStatusPaneL() ); 
	}

void CMdaAudioPlayerContainerView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_MDA_AUDIO_PLAYER_CONTAINER_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}
				
	}

void CMdaAudioPlayerContainerView::CleanupStatusPane()
	{
	}

/**
 *	Creates the top-level container for the view.  You may modify this method's
 *	contents and the CMdaAudioPlayerContainer::NewL() signature as needed to initialize the
 *	container, but the signature for this method is fixed.
 *	@return new initialized instance of CMdaAudioPlayerContainer
 */
CMdaAudioPlayerContainer* CMdaAudioPlayerContainerView::CreateContainerL()
	{
	return CMdaAudioPlayerContainer::NewL( ClientRect(), NULL, this );
	}
